package com.lishe.lishe.lishe.module.mall.adapter;


import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.module.mall.activity.ClassifySearchActivity;
import com.lishe.lishe.lishe.module.mall.bean.category.CategoryFirst;

import java.util.List;

/**
 * Created by yang on 2017/11/6.
 */

public class Classification2Adapter extends RecyclerviewBaseAdapter {
    private Activity mContext;
    private List<CategoryFirst> mList;

    public Classification2Adapter(Activity context, List<CategoryFirst> list){
        this.mContext=context;
        this.mList=list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolderMy(ViewGroup parent, int viewType) {
        View view= LayoutInflater.from(mContext).inflate(R.layout.item_classification2,null);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolderMy(RecyclerView.ViewHolder holder, final int position) {
        CategoryFirst category = mList.get(position);
        MyViewHolder viewHolder = (MyViewHolder) holder;
        if (position == 0) {
            viewHolder.topview.setVisibility(View.GONE);
        } else {
            viewHolder.topview.setVisibility(View.VISIBLE);
        }
        viewHolder.title.setText(category.getCate_name());
        viewHolder.recyclerview.setLayoutManager(new GridLayoutManager(mContext, 3));
        ClassificationItemAdapter adapter = new ClassificationItemAdapter(mContext, mList.get(position).getSon());
        viewHolder.recyclerview.setAdapter(adapter);
        adapter.setOnItemClickListener(new ItemListener.ItemClickListener() {
            @Override
            public void OnClickListener(RecyclerView.Adapter adapter, View view, int index) {
                Intent intent = new Intent(mContext, ClassifySearchActivity.class);
                intent.putExtra(ClassifySearchActivity.CATEID, mList.get(position).getSon().get(index).getCate_id());
                intent.putExtra(ClassifySearchActivity.TITLE, mList.get(position).getSon().get(index).getCate_name());
                mContext.startActivity(intent);
            }
        });
    }

    @Override
    public int getItemCountMy() {
        return mList.size();
    }

    @Override
    public int getItemViewTypeMy(int position) {
        return 0;
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView title;
        RecyclerView recyclerview;
        View topview;

        public MyViewHolder(View itemView) {
            super(itemView);
            title = itemView.findViewById(R.id.title);
            topview = itemView.findViewById(R.id.topview);
            recyclerview = itemView.findViewById(R.id.recyclerview);
        }
    }

}
